package com.pasu.breakreminder.subservices;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.pasu.breakreminder.R;
import com.pasu.breakreminder.utils.Logger;

public class SubServiceNotifications implements SubService {
    private NotificationManager mNotificationMan;
    private Context mContext;
    // Unique Identification Number for the Notification.
    // We use it on Notification start, and to cancel it.
    private int NOTIFICATION_ID = 1011012;
    private boolean mEnableSound;
    private boolean mEnableVibration;
    private boolean mEnableLed;

    public SubServiceNotifications(Context context, boolean sound, boolean vibration, boolean led) {
        mContext = context;
        mEnableSound = sound;
        mEnableVibration = vibration;
        mEnableLed = led;
        Logger.i("SubServiceNotifications", "notifications with: sound=" + sound + ", vibra=" + vibration + ", led=" + led);
    }

    /**
     * Show a notification
     */
    private void showNotification() {
        // In this sample, we'll use the same text for the ticker and the
        // expanded notification
        CharSequence text = "local_service_started";
        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
        // The PendingIntent to launch our activity if the user selects this
        // notification
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        if (mEnableSound) {
            notification.defaults |= Notification.DEFAULT_SOUND;
        }
        if (mEnableVibration) {
            notification.defaults |= Notification.DEFAULT_VIBRATE;
        }
        if (mEnableLed) {
            notification.defaults |= Notification.DEFAULT_LIGHTS;
        }
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, new Intent(), 0);
        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(mContext, "local_service_label", text, contentIntent);
        // Send the notification.
        mNotificationMan.notify(NOTIFICATION_ID, notification);
    }

    @Override
    public void startSubService() {
        mNotificationMan = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        // Display a notification about us starting. We put an icon in the
        // status bar.
        showNotification();
    }

    @Override
    public void stopSubService() {
        // Cancel the persistent notification.
        mNotificationMan.cancel(NOTIFICATION_ID);
    }
}
